import { ContentTypeEnum } from '@/enums/httpEnum';
import { defHttp } from '@/utils/http/axios';
import {
  CreateRoleParams,
  UpdateRoleParams,
  RoleParams,
  CreateRoleInterfaceParams,
  CreateRoleButtonParams,
} from './model/systemModel';

import { CreateRole, UpdateRole } from './model/roleModel';

enum Api {
  Role = '/role/',
  RoleList = '/role/list',
  setRoleStatus = '/role/change/status/',
  CreateRoleInterface = '/interface',
  GetRoleInterfaceList = '/interface/list',
  CreateRoleButton = '/button',
  GetRoleButtonList = '/button/list',
}

/**
 * 创建角色
 * @params CreateRole
 * @returns Promise
 */
export const create = (params?: CreateRole) =>
  defHttp.post(
    {
      url: Api.Role,
      headers: { 'Content-Type': ContentTypeEnum.FORM_DATA },
      params: params,
    },
    { isTransformResponse: false },
  );

export const update = (id: Number, params?: UpdateRole) =>
  defHttp.post(
    {
      url: `${Api.Role}${id}`,
      params: params,
      headers: { 'Content-Type': ContentTypeEnum.FORM_DATA },
    },
    { isTransformResponse: false },
  );

export const createRoleInterface = (params?: CreateRoleInterfaceParams) =>
  defHttp.post(
    {
      url: `/role/${params?.role_id}${Api.CreateRoleInterface}`,
      params: params,
      headers: { 'Content-Type': ContentTypeEnum.FORM_DATA },
    },
    { isTransformResponse: false },
  );

export const createRoleButton = (params?: CreateRoleButtonParams) =>
  defHttp.post(
    {
      url: `/role/${params?.role_id}${Api.CreateRoleButton}`,
      params: params,
      headers: { 'Content-Type': ContentTypeEnum.FORM_DATA },
    },
    { isTransformResponse: false },
  );

/**
 * 角色列表
 * @param params RoleParams
 * @returns
 */
export const getRoleList = (params?: RoleParams) =>
  defHttp.get({ url: Api.RoleList, params: params });

export const getRoleInterfaceList = (roleId: string) =>
  defHttp.get({ url: `/role/${roleId}${Api.GetRoleInterfaceList}` });

export const getRoleButtonList = (roleId: Number) =>
  defHttp.get({ url: `/role/${roleId}${Api.GetRoleButtonList}` });

/**
 * 修改角色状态
 * @param id 角色ID
 * @param status 状态
 * @returns
 */
export const setRoleStatus = (id: number, status: number) =>
  defHttp.post({
    url: Api.setRoleStatus + id,
    params: { status },
    headers: { 'Content-Type': ContentTypeEnum.FORM_DATA },
  });

export const deleteRoleIds = (ids: string) =>
  defHttp.delete<any>(
    {
      url: `${Api.Role}${ids}`,
      headers: { 'Content-Type': ContentTypeEnum.FORM_DATA },
      params: { ids },
    },
    { isTransformResponse: false },
  );
